package com.gusto.engine.semsim.measures.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

import com.gusto.engine.beans.exceptions.PropertyAccessException;
import com.gusto.engine.beans.model.Res;
import com.gusto.engine.semsim.exceptions.SimilarityException;
import com.gusto.engine.semsim.measures.ResourceSimilarity;
import com.gusto.engine.semsim.measures.ValueSimilarity;

/**
 * <p>The IdentitySimilarity returns 1.0 if the resources or values are similar and 0.0 otherwise.
 * It also allows to define a number of stop words, for which the identity doesn't apply.</p>
 * 
 * @author amokrane.belloui@gmail.com
 */
public class IdentitySimilarity implements ValueSimilarity, ResourceSimilarity {
	
	private Logger log = Logger.getLogger(IdentitySimilarity.class);
	
	/**
	 * List of words not to consider, 
	 * if two entities refers to a word like 'others' 
	 * it does's mean they're actually similar 
	 */
	private List<String> stopwords = new ArrayList<String>();
	public void setStopwords(List<String> stopwords) {
		this.stopwords = stopwords;
		log.debug(stopwords);
	}
	
	public String getId() {
		String id = "[" + getClass().getName() + "|" + stopwords + "]";
		return id;
	}
	
	public Double getSimilarity(Object value1, Object value2) throws SimilarityException {
		if (value1.equals(value2)) {
			if (!stopwords.contains(value1)) {
				log.warn("Identical " + value1 + " " + value2);
				return 1.0;
			}
			else {
				log.debug("Identical but stop word " + value1 + " " + value2);
			}
		}
		else {
			log.debug("Not identical : '" + value1 + "' <> '" + value2 + "'");
		}
		return 0.0;
	}
	
	public Double getSimilarity(Res res1, Res res2) throws SimilarityException, PropertyAccessException {
		if (res1.equals(res2)) {
			if (!stopwords.contains(res1.toString())) {
				log.warn("Identical " + res1 + " " + res2);
				return 1.0;
			}
			else {
				log.debug("Identical but stop word " + res1 + " " + res2);
			}
		}
		else {
			log.debug("Not identical : '" + res1 + "' <> '" + res2 + "'");
		}
		return 0.0;
	}
	
}
